/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spakehelpdesk.bean;

import com.spakehelpdesk.entity.Account;
import com.spakehelpdesk.entity.Department;
import com.spakehelpdesk.entity.Role;
import com.spakehelpdesk.java.EncryptionMd5;
import com.spakehelpdesk.java.MailConfig;
import com.spakehelpdesk.java.SessionManage;
import com.spakehelpdesk.java.URLManage;
import com.spakehelpdesk.session.AccountSession;
import com.spakehelpdesk.session.DepartmentSession;
import com.spakehelpdesk.session.RoleSession;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.mail.MessagingException;

/**
 *
 * @author leehoanh
 */
@SessionScoped
public class AccountBean implements Serializable {

    @EJB
    private RoleSession roleSession;
    @EJB
    private DepartmentSession departmentSession;
    @EJB
    private AccountSession accountSession;
    private SessionManage session;
    private Account selectAccount; //Account is selected for show detail and edit
    private Account selectAccountName;//Account is select in from tableSeachbyName
    private Account newAccount = new Account();//Account is created
    private int roleId = 1;
    private int departmentId = 1;
    private List<Account> filteAccounts;
    private List<Account> accounts;
    public MailConfig mail = new MailConfig();

    public List<Account> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }

    public List<Account> getFilteAccounts() {
        return filteAccounts;
    }

    public void setFilteAccounts(List<Account> filteAccounts) {
        this.filteAccounts = filteAccounts;
    }

    public Account getSelectAccountName() {
        return selectAccountName;
    }

    public void setSelectAccountName(Account selectAccountName) {
        this.selectAccountName = selectAccountName;
    }

    //get value of departmentId
    public int getDepartmentId() {
        return departmentId;
    }
    //set value for departmentId

    public void setDepartmentId(int departmentId) {
        this.departmentId = departmentId;
    }
    //get value of roleId

    public int getRoleId() {
        return roleId;
    }
    //set value for roleId

    public void setRoleId(int roleId) {
        this.roleId = roleId;
    }
    //get value of newAccount

    public Account getNewAccount() {
        return newAccount;
    }
    //set value for newAccount

    public void setNewAccount(Account newAccount) {
        this.newAccount = newAccount;
    }
    //get value of selectAccount

    public Account getSelectAccount() {
        return selectAccount;
    }
    //set value for selectAccount

    public void setSelectAccount(Account selectAccount) {
        this.selectAccount = selectAccount;
    }

    /**
     * Creates a new instance of AccountBean
     */
    public AccountBean() {
    }

    //show detail of user
    public Account getProfile() {
        return accountSession.getAccountById(session.getValue("accountId").toString());
    }

    //show all account is sorted
    public List<Account> getAllAccount() {
        if (session.getValue("loadaccount") == null) {
            accounts = accountSession.getAll();
            session.setValue("loadaccount", "loaded");
        }
        return accounts;

    }

    //forward to ViewDetailAccount page
    public void viewDetailAcc() {
        URLManage.sendRediret("Admin/ViewAccount.xhtml");
    }
    //forward to ViewDetailAccount page

    public void viewDetailAccSeacrh() {
        selectAccount = selectAccountName;
        URLManage.sendRediret("Admin/ViewAccount.xhtml");
    }

    //get list SelectItem about Department
    public SelectItem[] getDepartements() {
        List<Department> depart = departmentSession.getAll();
        SelectItem[] department = null;
        if (depart != null) {
            department = new SelectItem[depart.size() + 1];
            department[0] = new SelectItem("", "Select");
            for (int i = 0; i < depart.size(); i++) {
                department[i + 1] = new SelectItem(depart.get(i).getDepartmentID(), depart.get(i).getDepartmentName());
            }
        }
        return department;
    }
    //get list SelectItem about Role

    public SelectItem[] getRoles() {
        List<Role> rol = roleSession.getAll();
        SelectItem[] roles = null;
        if (rol != null) {
            roles = new SelectItem[rol.size()];
            for (int i = 0; i < rol.size(); i++) {
                roles[i] = new SelectItem(rol.get(i).getRoleID(), rol.get(i).getRoleName());
            }
        }
        return roles;
    }
    //remove account

    public void remove() {
        System.out.println("****" + selectAccount.getAccountID());
        accountSession.remove(selectAccount);
        if (session.getValue("loadaccount") != null) {
            session.remove("loadaccount");
        }
    }
    //back page

    public void back() {
        if (session.getValue("loadaccount") != null) {
            session.remove("loadaccount");
        }

        URLManage.sendRediret("Admin/ManageAccount.xhtml");


    }
    //get all by seach account

    public List<Account> getSearchAcc() {
        filteAccounts = accountSession.getAll();
        return filteAccounts;
    }
    //update or edit Acount

    public void update() {
        selectAccount.setDepartmentID(new Department(selectAccount.getDepartmentID().getDepartmentID()));
        selectAccount = accountSession.updateAccount(selectAccount);
    }
    //forward to CreateAccount page

    public void create() {
        URLManage.sendRediret("Admin/CreateNewAccount.xhtml");
    }
    //Logout method

    public void logout() {
        if (session.getValue("roleID") != null) {
            session.remove("roleID");
            URLManage.sendRediret("login.xhtml");
        }
    }
    //Create new account

    public void toProfile() {
        URLManage.sendRediret("Admin/ProfileAdmin.xhtml");
    }

    //create the new
    public void createNew() {
        boolean result = false;
        newAccount.setPassword(EncryptionMd5.Encryption("0000"));//set password defauld that is encrypted
        newAccount.setRoleID(new Role(roleId));
        newAccount.setDepartmentID(new Department(departmentId));
        try {
            mail.sendEmail(mail.createMessage("drleehoanh@gmail.com", newAccount.getEmail(), "Notification new account", "Dear Madam/Sir!\n You are create a account with AccountId:" + newAccount.getAccountID() + " and password is:0000"));
        } catch (MessagingException ex) {
            Logger.getLogger(AccountBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = accountSession.createNewAccount(newAccount);
        if (result) {
            back();
        } else {
            URLManage.sendRediret("errorLogin.xhtml");
        }
    }
    
    public void updateAccount(){
        accountSession.updateAccount(selectAccount);
    }
    
    public void toViewProfile(){
        URLManage.sendRediret("Employee/ProfileEmployee.xhtml");
    }
    
    public void toHome(){
        URLManage.sendRediret("Employee/Home.xhtml");
    }
}
